home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / pdf_draw.ps < prev    next >
Encoding:
Text File  |  1997-03-06  |  9.2 KB  |  364 lines

  1. %    Copyright (C) 1994, 1995, 1997 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % pdf_draw.ps
  16. % PDF drawing operations (graphics, text, and images).
  17.  
  18. % We don't handle the following PDF elements yet (identified by
  19. % page number in the reference manual):
  20. %    style strings (63-64), except in a few known fonts
  21. %    font descriptor resources (71-75), except for MissingWidth
  22. %    text clipping modes (104)
  23. %        What do these mean??
  24.  
  25. % We handle the following PDF 1.2 constructs:
  26. %    gs operator (but not functions)
  27.  
  28. /.setlanguagelevel where { pop 2 .setlanguagelevel } if
  29. .currentglobal true .setglobal
  30. /pdfdict where { pop } { /pdfdict 100 dict def } ifelse
  31. GS_PDF_ProcSet begin
  32. pdfdict begin
  33.  
  34. % For simplicity, we use a single interpretation dictionary for all
  35. % PDF graphics operations, even though this is too liberal.
  36. /drawopdict 100 dict def
  37.  
  38. % ================================ Graphics ================================ %
  39.  
  40. % ---------------- Graphics state management ---------------- %
  41.  
  42. drawopdict begin
  43.             % Graphics state stack
  44.   /q { q } def
  45.   /Q { Q } def
  46.             % Graphics state setting
  47.   /cm { cm } def
  48.   /i { i } def
  49.   /J { J } def
  50.   /d { d } def
  51.   /j { j } def
  52.   /w { w } def
  53.   /M { M } def
  54.   /gs { gs } def
  55. end
  56.  
  57. /gsparamdict mark
  58.   /Type { pop }
  59.   /SA { /setstrokeadjust 1 # }
  60.   /OP { /setoverprint 1 # }
  61.   /BG
  62.    { dup /Identity eq
  63.       { pop { } /setblackgeneration 1 #
  64.       }
  65.       {        %****** DUMMY ******
  66.     pop
  67.       }
  68.      ifelse
  69.    }
  70.   /UCR
  71.    { dup /Identity eq
  72.       { pop { } /setundercolorremoval 1 #
  73.       }
  74.       {        %****** DUMMY ******
  75.     pop
  76.       }
  77.      ifelse
  78.    }
  79.   /TR
  80.    { dup /Identity eq
  81.       { pop { } /settransfer 1 #
  82.       }
  83.       {        %****** DUMMY ******
  84.     pop
  85.       }
  86.      ifelse
  87.    }
  88.   /HT
  89.    { dup /Default eq
  90.       { pop /.setdefaultscreen 0 #
  91.       }
  92.       {        %****** DUMMY ******
  93.     pop
  94.       }
  95.      ifelse
  96.    }
  97.   /HTP { oget aload pop /sethalftonephase 2 # }
  98. .dicttomark readonly def
  99. /gs            % <gsres> gs -
  100.  { Page /ExtGState rget
  101.     {  { oforce exch gsparamdict exch .knownget { exec } { pop } ifelse }
  102.       forall
  103.     }
  104.    if
  105.  } bdef
  106.  
  107. % ---------------- Color setting ---------------- %
  108.  
  109. /csgray            % <op> <csop> csgray -
  110.  { /DefaultGray Page /ColorSpace rget
  111.     { 3 -1 roll exch 3 1 roll }
  112.    if pop cvx exec
  113.  } bdef
  114. /csrgb            % <op> <csop> csrgb -
  115.  { /DefaultRGB Page /ColorSpace rget
  116.     { 3 -1 roll exch 3 1 roll }
  117.    if pop cvx exec
  118.  } bdef
  119. /cscmyk            % <op> <csop> cscmyk -
  120.  { pop cvx exec
  121.  } bdef
  122. /csresolve        % <csresource> csresolve <colorspace>
  123.  { dup Page /ColorSpace rget
  124.     { exch pop resolvecolorspace }
  125.     { /undefined cvx signalerror }
  126.    ifelse dup type /nametype ne { dup length 1 eq { 0 get } if } if
  127.  } bdef
  128. /cset        % <c0> ... [- <sc1> - <sc3> <sc4>] <colorspace> cset -
  129.  { gsave setcolorspace mark currentcolor counttomark
  130.    grestore dup 2 add 1 roll cleartomark get exec
  131.  } bdef
  132.  
  133. %**************** sc and SC don't work, because the color space information
  134. %**************** isn't available at conversion time.
  135. drawopdict begin
  136.   /g { /g { cs sc1 } csgray } bdef
  137.   /rg { /rg { cs sc3 } csrgb } bdef
  138.   /k { /k { cs sc4 } cscmyk } bdef
  139.   /cs { csresolve cs } bdef
  140.   /sc { { null sc1 null sc3 sc4 } FillColorSpace cset } bdef
  141.   /G { /G { CS SC1 } csgray } bdef
  142.   /RG { /RG { CS SC3 } csrgb } bdef
  143.   /K { /K { CS SC4 } cscmyk } bdef
  144.   /CS { csresolve CS } bdef
  145.   /SC { { null SC1 null SC3 SC4 } StrokeColorSpace cset } bdef
  146. end
  147.  
  148. % ---------------- Paths ---------------- %
  149.  
  150. drawopdict begin
  151.             % Path construction
  152.   /m { m } def
  153.   /l { l } def
  154.   /c { c } def
  155.   /v { v } def
  156.   /y { y } def
  157.   /re { re } def
  158.   /h { h } def
  159.             % Path painting and clipping
  160.   /n { n } def
  161.   /S { S } def
  162.   /s { s } def
  163.   /f { f } def
  164.   /f* { f* } def
  165.   /B { B } def
  166.   /b { b } def
  167.   /B* { B* } def
  168.   /b* { b* } def
  169.   /W { W } def
  170.   /W* { W* } def
  171. end
  172.  
  173. % ---------------- XObjects ---------------- %
  174.  
  175. /xobjectprocs mark        % <dict> -proc- -
  176.   /Image { DoImage }
  177.   /Form { DoForm }
  178. .dicttomark readonly def
  179.  
  180. /ncompdict mark
  181.   /DeviceGray 1
  182.   /CalGray 1
  183.   /DeviceRGB 3
  184.   /CalRGB 3
  185.   /Lab 3
  186.   /DeviceCMYK 4
  187.   /CalCMYK 4
  188. .dicttomark readonly def
  189. /defaultdecodedict mark
  190.   /DeviceGray [0 1] readonly
  191.   /CalGray 1 index
  192.   /DeviceRGB [0 1 0 1 0 1] readonly
  193.   /CalRGB 1 index
  194.   /Lab
  195.    { 0 100 2 index 1 get /Range .knownget not { {-100 100 -100 100} } if
  196.      aload pop 6 array astore readonly
  197.    } bind
  198.   /DeviceCMYK [0 1 0 1 0 1 0 1] readonly
  199.   /CalCMYK 1 index
  200. .dicttomark readonly def
  201. /resolvecolorspace    % <cspace> resolvecolorspace <cspace>
  202.  { dup type /arraytype eq
  203.     { dup 0 get /Indexed eq
  204.        { dup 3 oget dup type /stringtype eq
  205.       { pop
  206.       }
  207.       {    % The color lookup table is a stream.
  208.         % Get its contents.
  209.         true resolvestream
  210.         1 index 2 get 1 add
  211.         ncompdict 3 index 1 get
  212.           dup type /arraytype eq { 0 get } if get mul
  213.         string readstring pop
  214.         1 index 3 3 -1 roll put
  215.       }
  216.      ifelse
  217.        }
  218.       if
  219.     }
  220.    if
  221.  } bdef
  222. /DoImage
  223.  { dup length dict
  224.    1 index /ColorSpace knownoget
  225.     { resolvecolorspace
  226.       dup type /arraytype eq { dup length 1 eq { 0 get } if } if
  227.       exch begin /ColorSpace exch def
  228.     }
  229.     { begin
  230.     }
  231.    ifelse
  232.    /ImageType 1 def
  233.         % Always define ImageMask appropriately.
  234.    dup /ImageMask knownoget dup { and } if
  235.      /ImageMask exch def
  236.    /Width 2 copy oget def
  237.    /Height 2 copy oget def
  238.    /BitsPerComponent 2 copy oget def
  239.    /Decode 2 copy knownoget not
  240.     {        % Decode is required for the PostScript image operators.
  241.       ImageMask
  242.        { [0 1]
  243.        }
  244.        { ColorSpace dup type /arraytype eq { 0 get } if dup /Indexed eq
  245.       { pop [ 0 1 BitsPerComponent bitshift 1 sub ] }
  246.       { defaultdecodedict exch get exec }
  247.      ifelse
  248.        }
  249.       ifelse
  250.     }
  251.    if def
  252.    /Interpolate 2 copy knownoget { def } { pop } ifelse
  253.    /ImageMatrix Width 0 0 Height neg 0 Height 6 array astore def
  254.         % Define DataSource as the width of the row buffer,
  255.         % which is what is needed if we're writing PostScript.
  256.    /DataSource 
  257.      Width BitsPerComponent mul
  258.      ImageMask not { Decode length 2 idiv mul } if
  259.      7 add 8 idiv
  260.    def
  261.         % Even though we're going to read data,
  262.         % pass false to resolvestream so that
  263.         % it doesn't try to use Length (which may not be present).
  264.    false resolvestream /Is_stream exch store
  265.    currentdict end ID
  266.  } bdef
  267. % Redefine Is, which constructs the data source for the image,
  268. % to retrieve the stream.  (pdf_2ps.ps redefines Is to copy the data too.)
  269. userdict /Is_stream null put
  270. /Is        % <imagedict> Is <imagedict> <datasource>
  271.  { Is_stream
  272.  } bdef
  273.  
  274. /DoForm
  275.  { dup [ /pop load 2 index
  276.     { false resolvestream pdfopdict .pdfrun }
  277.    aload pop ] cvx /PaintProc exch put
  278.    execform
  279.  } bdef
  280.  
  281. drawopdict begin
  282.   /Do
  283.     { PDFfile fileposition exch
  284.       dup Page /XObject rget not { /undefined cvx signalerror } if
  285.       exch pop dup /Subtype get xobjectprocs exch get exec
  286.       PDFfile exch setfileposition
  287.     } bdef
  288. end
  289.  
  290. % ---------------- In-line images ---------------- %
  291.  
  292. % Undo the abbreviations in an in-line image dictionary.
  293. % Note that these can appear as keys, values, or members of array values.
  294. % /I is ambiguous; we check specially for it below.
  295. /unabbrevdict mark
  296.     % Top-level dictionary keys
  297.   /BPC /BitsPerComponent  /CS /ColorSpace  /D /Decode  /DP /DecodeParms
  298.   /F /Filter  /H /Height  /IM /ImageMask  /W /Width
  299.     % Values
  300.   /AHx /ASCIIHexDecode  /A85 /ASCII85Decode  /CC /CalCMYK
  301.   /CCF /CCITTFaxDecode  /CG /CalGray  /CR /CalRGB
  302.   /DCT /DCTDecode  /CMYK /DeviceCMYK  /G /DeviceGray  /RGB /DeviceRGB
  303.   /I /Indexed  /LZW /LZWDecode  /RL /RunLengthDecode
  304. .dicttomark readonly def
  305. /unabbrev        % <obj> unabbrev <obj'>
  306.  { dup type /nametype eq
  307.     { unabbrevdict 1 index .knownget { exch pop } if
  308.     }
  309.     { dup type /arraytype eq
  310.        { dup 0 1 2 index length 1 sub
  311.       { 2 copy get unabbrev put dup
  312.       }
  313.      for pop
  314.        }
  315.       if
  316.     }
  317.    ifelse
  318.  } bdef
  319.  
  320. drawopdict begin
  321.   /BI { mark } bdef
  322.   /ID
  323.     { counttomark
  324.        { counttomark 1 roll
  325.      dup /I eq { pop /Interpolate } { unabbrev } ifelse
  326.        }
  327.       repeat
  328.       /File PDFsource
  329.       .dicttomark DoImage
  330.       PDFsource token pop /EI ne { /ID cvx /syntaxerror signalerror } if
  331.     } bdef
  332. end
  333.  
  334. % ================================ Text ================================ %
  335.  
  336. drawopdict begin
  337.             % Text control
  338.   /BT { BT } def
  339.   /ET { ET } def
  340.   /Tc { Tc } def
  341.   /TL { TL } def
  342.   /Tr { Tr } def
  343.   /Ts { Ts } def
  344.   /Tw { Tw } def
  345.   /Tz { Tz } def
  346.             % Text positioning
  347.   /Td { Td } def
  348.   /TD { TD } def
  349.   /Tm { Tm } def
  350.   /T* { T* } def
  351.             % Text painting
  352.   /Tj { Tj } def
  353.   /' { ' } def
  354.   /" { " } def
  355.   /TJ { mark exch aload pop TJ } def
  356. end
  357.  
  358. end            % pdfdict
  359. end            % GS_PDF_ProcSet
  360. .setglobal
  361.